package com.woniu.finance.service.impl;

import com.alibaba.fastjson.JSON;
import com.woniu.finance.entity.Menu;
import com.woniu.finance.mapper.MenuMapper;
import com.woniu.finance.service.IMenuService;
import com.woniu.util.utils.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘品水
 * @since 2023-04-25
 */
@Service
public class MenuServiceImpl   implements IMenuService {
    @Autowired(required = false)
    private MenuMapper menuMapper;

    @Autowired(required = false)
    private StringRedisTemplate redisTemplate;
    @Override
    public HttpResult<?> findAllMenu() {
        String userStr = redisTemplate.opsForValue().get("menus:");
        //是否有缓存
        if(userStr != null || "".equals(userStr)){
            List<Menu> menu = JSON.parseArray(userStr,Menu.class);
            return new HttpResult<>().ok(menu);

        }else{
            //没有就把查到的存进去
            List<Menu> menu = menuMapper.findAllMenu();
            if (menu == null) {
                redisTemplate.opsForValue().set("menu","",5, TimeUnit.SECONDS);
            }else{
                redisTemplate.opsForValue().set("menu",JSON.toJSONString(menu));
            }
            List<Menu> menus = getMenuNodes(menu);
            return new HttpResult<>().ok(menus);
        }
    }

    @Override
    public HttpResult<?> saveMenu(Menu menu) {
        try {
            menuMapper.insert(menu);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> modifyMenu(Menu menu) {
        try {
            menuMapper.updateById(menu);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> removeMenuById(Integer id) {
        try {
            int delete = menuMapper.deleteById(id);
            return new HttpResult<>().ok();
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> findMenuById(Integer id) {
        try {
            Menu menu = menuMapper.selectById(id);
            return new HttpResult<>().ok(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new HttpResult<>().fail();
        }
    }

    @Override
    public HttpResult<?> queryFinanceMenusByUserName(String userName) {
        String userStr = redisTemplate.opsForValue().get("menus:");
        //是否有缓存
        if(userStr != null || "".equals(userStr)){
            List<Menu> menu = JSON.parseArray(userStr,Menu.class);
            return new HttpResult<>().ok(menu);

        }else{
            //没有就把查到的存进去
            List<Menu> menu = menuMapper.queryFinanceMenusByUserName(userName);
            if (menu == null) {
                redisTemplate.opsForValue().set("menu","",5, TimeUnit.SECONDS);
            }else{
                redisTemplate.opsForValue().set("menu",JSON.toJSONString(menu));
            }
            List<Menu> menus = getMenuNodes(menu);
            return new HttpResult<>().ok(menus);
        }
    }

    public List<Menu> getMenuNodes(List<Menu> menus){
        List<Menu> parentMenus = menus.stream().filter(e -> e.getParentMenuId() == null)
                .collect(Collectors.toList());
        for (Menu parentMenu: parentMenus) {
            List<Menu> childrenMenus = new ArrayList<>();
            for (Menu menu : menus){
                if(parentMenu.getId().equals(menu.getParentMenuId())){
                    childrenMenus.add(menu);
                }
            }
            parentMenu.setChildrenMenu(childrenMenus);
        }
        return parentMenus;
    }
}
